﻿using Bll;
using DecorationSys.Models;
using Entity;
using IBll;
using Microsoft.AspNetCore.Mvc;
using System.Security.Principal;

namespace RepositorySys.Controllers
{
    /// <summary>
    /// 角色相关
    /// </summary>
    public class RoleInfoController : Controller
    {
        IRoleInfoBll _roleInfoBll;
        IUserInfoBll _userInfoBll;

        public RoleInfoController(IRoleInfoBll roleInfoBll, IUserInfoBll userInfoBll)
        {
            _roleInfoBll = roleInfoBll;
            _userInfoBll = userInfoBll;
        }

        /// <summary>
        /// 角色列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult RoleInfoListView()
        {
            return View();
        }

        /// <summary>
        /// 添加角色视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateRoleInfoView()
        {
            return View();
        }

        /// <summary>
        /// 修改角色视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateRoleInfoView()
        {
            return View();
        }

        /// <summary>
        /// 角色绑定用户视图
        /// </summary>
        /// <returns></returns>
        public IActionResult BindUserInfoView()
        {
            return View();
        }

        /// <summary>
        /// 角色绑定菜单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult BindMenuInfoView()
        {
            return View();
        }

        

        /// <summary>
        /// 根据角色id获取角色信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetRoleInfoById(string roleId)
        {
            RoleInfo RoleInfo = _roleInfoBll.GetRoleInfoById(roleId);

            if (RoleInfo == null)
            {
                return MyJsonResult.GetErrorResult("角色不存在");
            }
            else
            {
                return MyJsonResult.GetSuccessResult(new   //mapping
                {
                    RoleInfo.RoleName,
                    RoleInfo.Description
                });
            }
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UpdateRoleInfo(string roleId, string roleName, string description)
        {
            //参数验证
            if (string.IsNullOrEmpty(roleId))
            {
                return MyJsonResult.GetErrorResult("角色id不能为空");
            }
            if (string.IsNullOrEmpty(roleName))
            {
                return MyJsonResult.GetErrorResult("角色名不能为空");
            }

            string msg;
            //调用业务逻辑层
            bool isSuccess = _roleInfoBll.UpdateRoleInfo(roleId, roleName, description,out msg);
            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }


        /// <summary>
        /// 获取角色列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetRoleInfoList(string roleName, int page, int limit)
        {
            if (page <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "页码有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            if (limit <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "每页条数有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            int count;

            //查角色数据
            var list = _roleInfoBll.GetRoleInfoList(roleName, page, limit, out count);

            return new
            {
                code = 0,
                msg = "cg",
                count = count,
                data = list
            };
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult CreateRoleInfo(string roleName, string description)
        {
            //参数验证
            if (string.IsNullOrEmpty(roleName))
            {
                return MyJsonResult.GetErrorResult("请输入角色名称");
            }

            //创建业务逻辑层
            //RoleInfoBll RoleInfoBll = new RoleInfoBll();

            string msg;
            bool isSuccess = _roleInfoBll.CreateRoleInfo(roleName, description, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteRoleInfo(string roleId)
        {
            if (string.IsNullOrEmpty(roleId))
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _roleInfoBll.DeleteRoleInfo(roleId);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteRoleInfos(List<string> roleIds)
        {
            if (roleIds.Count <= 0 || roleIds == null)
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _roleInfoBll.DeleteRoleInfos(roleIds);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 为角色绑定用户
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="UserIds"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult BindUserInfo(string roleId, List<string> userIds)
        {
            if (string.IsNullOrEmpty(roleId))
            {
                return MyJsonResult.GetErrorResult("角色id不能为空");
            }

            if (userIds == null || userIds.Count() == 0)
            {
                return MyJsonResult.GetErrorResult("用户id不能为空");
            }

            bool isSuccess = _roleInfoBll.BindUserInfo(roleId, userIds);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }


        /// <summary>
        /// 为角色解除绑定用户
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UnbindUserInfo(string roleId, List<string> userIds)
        {
            if (string.IsNullOrEmpty(roleId))
            {
                return MyJsonResult.GetErrorResult("角色id不能为空");
            }

            if (userIds == null || userIds.Count() == 0)
            {
                return MyJsonResult.GetErrorResult("用户id不能为空");
            }

            bool isSuccess = _roleInfoBll.UnbindUserInfo(roleId, userIds);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 获取穿梭框的备选数据，和已绑定的用户数据
        /// </summary>
        /// <returns></returns>
        public MyJsonResult GetBindUserInfo(string roleId)
        {
            var datas =  _roleInfoBll.GetBindUserInfo(roleId);
            return MyJsonResult.GetSuccessResult(datas);
        }

        /// <summary>
        /// 为角色绑定菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="menuIds"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult BindMenuInfo(string roleId, List<string> menuIds)
        {
            if (string.IsNullOrEmpty(roleId))
            {
                return MyJsonResult.GetErrorResult("角色id不能为空");
            }

            if (menuIds == null || menuIds.Count() == 0)
            {
                return MyJsonResult.GetErrorResult("菜单id不能为空");
            }

            bool isSuccess = _roleInfoBll.BindMenuInfo(roleId, menuIds);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }


        /// <summary>
        /// 为角色解除绑定菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="menuIds"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UnbindMenuInfo(string roleId, List<string> menuIds)
        {
            if (string.IsNullOrEmpty(roleId))
            {
                return MyJsonResult.GetErrorResult("角色id不能为空");
            }

            if (menuIds == null || menuIds.Count() == 0)
            {
                return MyJsonResult.GetErrorResult("菜单id不能为空");
            }

            bool isSuccess = _roleInfoBll.UnbindMenuInfo(roleId, menuIds);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 获取穿梭框的备选数据，和已绑定的菜单数据
        /// </summary>
        /// <returns></returns>
        public MyJsonResult GetBindMenuInfo(string roleId)
        {
            var datas = _roleInfoBll.GetBindMenuInfo(roleId);
            return MyJsonResult.GetSuccessResult(datas);
        }
    }

}
